package com.quan.sdk.weixin.tools;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quan.sdk.weixin.common.WeixinContext;
import com.quan.sdk.weixin.common.utils.WeixinUtil;
import com.quan.sdk.weixin.model.SubscribeUser;
import com.quan.sdk.weixin.model.UserDetail;

/**
 * 微信用户工具类
 * @author yhaoquan
 *
 */
public class UserTools {

	private static Logger logger = LoggerFactory.getLogger(UserTools.class);
	
	/**
	 * 根据openid获取用户详情
	 * <br>方式：GET
	 * <br>参数：
	 * <br>&nbsp;&nbsp;&nbsp; access_token 全局唯一接口调用凭据
	 * <br>&nbsp;&nbsp;&nbsp; openid	普通用户的标识，对当前公众号唯一
	 */
	public static String USER_DETAIL_URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN" ;
	
	/**
	 * 获取用户列表
	 * <br>方式：GET
	 * <br>参数：
	 * <br>&nbsp;&nbsp;&nbsp; access_token 全局唯一接口调用凭据
	 * <br>&nbsp;&nbsp;&nbsp; next_openid	第一个拉取的OPENID，不填默认从头开始拉取
	 */
	public static String USER_LIST_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s" ;
	
	/**
	 * 获取用户列表
	 * @param accessToken
	 * @param next_openid
	 * @return
	 */
	public static SubscribeUser getSubscribeUserList(String accessToken, String next_openid) {
		if(StringUtils.isEmpty(accessToken)) {
			accessToken = WeixinContext.getAccessToken() ;
		}
		if(StringUtils.isEmpty(accessToken)) {
			logger.info("获取用户列表需要微信全局唯一凭证AccessToken");
			return null;
		}
		
		String url = String.format(USER_LIST_URL, accessToken, StringUtils.isNotBlank(next_openid) ? next_openid : "") ;
		String asString = WeixinUtil.doGet(url).asString() ;
		SubscribeUser subscribeUser = JSON.parseObject(asString, SubscribeUser.class) ;
		
		if(subscribeUser.getData() != null) {
			JSONObject parseObject2 = JSON.parseObject(asString);
			JSONObject parseObject3 = JSON.parseObject(parseObject2.get("data").toString());
			JSONArray parseArray = JSON.parseArray(parseObject3.get("openid").toString()) ;
			List<String> openids = new ArrayList<String>() ;
			for (Object openid : parseArray) {
				openids.add(openid.toString());
			}
			subscribeUser.setData(openids);
		}
		
		return subscribeUser;
	}
	
	/**
	 * 获取用户列表
	 * @param next_openid
	 * @return
	 */
	public static SubscribeUser getSubscribeUserList(String next_openid) {
		return getSubscribeUserList(null, next_openid) ;
	}

	/**
	 * 获取用户基本信息(UnionID机制)
	 * @param accessToken
	 * @param openid
	 * @return
	 */
	public static UserDetail getUserDetail(String accessToken, String openid) {
		if(StringUtils.isEmpty(accessToken)) {
			accessToken = WeixinContext.getAccessToken() ;
		}
		if(StringUtils.isEmpty(accessToken)) {
			logger.info("获取用户列表需要微信全局唯一凭证AccessToken");
			return null;
		}
		String url = String.format(USER_DETAIL_URL, accessToken, openid) ;
		UserDetail detail = WeixinUtil.doGet(url).asBean(UserDetail.class) ;
		return detail;
	}
	
	/**
	 * 获取用户基本信息(UnionID机制)
	 * @param openid
	 * @return
	 */
	public static UserDetail getUserDetail(String openid) {
		return getUserDetail(null, openid) ;
	}
	
}
